package xyz.xiezc.spider.prase;

import cn.hutool.setting.Setting;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import xyz.xiezc.ioc.annotation.Component;
import xyz.xiezc.ioc.annotation.Inject;
import xyz.xiezc.spider.common.Request;
import xyz.xiezc.spider.common.XBean;
import xyz.xiezc.spider.common.XUtil;
import xyz.xiezc.spider.entity.MzituBean;

import java.util.regex.Pattern;

/**
 * 首页和 翻页
 *
 * @author wb-xzc291800
 * @date 2019/04/17 18:13
 */
@Slf4j
@Component
public class MenuHtmlParseImpl implements ParseResponse {

    @Getter
    @Setter
    @Inject
    Setting setting;

    /**
     * 首页
     */
    Pattern menuPattern = Pattern.compile("https://www.mzitu.com/(\\w+?)/");
    /**
     * 分页
     */
    Pattern menuPagePattern = Pattern.compile("https://www.mzitu.com/(\\w+?)/page/\\w+?/");

    @Override
    public int orderNumber() {
        return 10;
    }

    @Override
    public XBean parse(XBean xBean) {
        Request request = xBean.getRequest();
        String url = request.getUrl();
        //匹配上了
        if (menuPagePattern.matcher(url).matches() || menuPattern.matcher(url).matches()) {
            praseMenuHtml(xBean, request);
            xBean.setStatus(3);
        }
        return xBean;
    }


    /**
     * 解析 列表目录页的链接
     *
     * @param xBean
     * @param request
     */
    private void praseMenuHtml(XBean xBean, Request request) {

        Document document = XUtil.getDocument(xBean.getResponse());
        String type = document.select("body > div.main > div.main-content > div.currentpath").text();

        Elements select = document.select("body > div.main > div.main-content > div.postlist");
        Elements li = select.select("li");
        li.forEach(element -> {
            String s = element.className();
            if (s.equals("box")) {
                return;
            }
            MzituBean mzituBean = new MzituBean();
            String[] split = type.split("»");
            if (split == null || split.length < 2) {
                mzituBean.setType(type);
            } else {
                mzituBean.setType(type.split("»")[1]);
            }
            Elements span = element.getElementsByTag("span");
            Elements eq = span.eq(0);
            Elements eq1 = span.eq(1);
            mzituBean.setTitle(eq.text());
            mzituBean.setPublishTime(eq1.text());

            String href = element.getElementsByTag("a").attr("href");
            mzituBean.setFetchUrl(href);

            Request request1 = new Request();
            request1.setUrl(XUtil.getComplieUrl(href, request));
            request1.addExtra(request.getExtra());
            request1.addExtra("title", mzituBean.getTitle());
            xBean.addNextRequest(request1);
            xBean.addBean(mzituBean);
        });

        //下一页
        Elements select1 = document.select("body > div.main > div.main-content > div.postlist > nav > div > a");
        select1.forEach(element -> {
            String href = XUtil.getComplieUrl(element.attr("href"), request);
            Request request1 = new Request();
            request1.setUrl(href);
            request1.setExtra(request.getExtra());
            xBean.addNextRequest(request1);
        });

    }

}
